package com.zemb.flxwear.Adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zemb.flxwear.Bean.CategoryBean;
import com.zemb.flxwear.R;

import java.util.ArrayList;
import java.util.List;

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements View.OnClickListener {
    private List<CategoryBean> list;
    private Context context;

    public CategoryAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_category, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        CategoryBean category = list.get(position);
        viewHolder.tv_category_name.setText(category.getName() + "");
        viewHolder.iv_category.setImageDrawable(context.getResources().getDrawable(category.imageId));
        viewHolder.v_top_panel.setTag(position);
        viewHolder.v_top_panel.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setList(List<CategoryBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();      //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (view.getId()) {
                case R.id.v_top_panel:
                    mOnItemClickListener.onItemClick(view, list.get(position), position);
                    break;
                default:
                    //  mOnItemClickListener.onItemClick(view, DriveAdapter.ViewName.ITEM, position);
                    break;
            }
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        //这些控件都可以设置监听事件
        private TextView tv_category_name;
        private ImageView iv_category;
        private View v_top_panel;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_category_name = itemView.findViewById(R.id.tv_category_name);
            v_top_panel = itemView.findViewById(R.id.v_top_panel);
            iv_category = itemView.findViewById(R.id.iv_category);
            v_top_panel.setOnClickListener(CategoryAdapter.this);
        }
    }

    //自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener {
        void onItemClick(View v,CategoryBean categoryBean, int position);

    }

    private OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}

